#include <bits/stdc++.h>
using namespace std;
const int N = 1e5 + 7, M = 2e5 + 7;
int n, m;
int par[N];
void init() {
  for (int i = 0; i <= n; ++i) par[i] = i;
}
int find(int x) { return par[x] = (x == par[x] ? x : find(par[x])); }
struct Edge {
  int a, b, w;
  bool operator<(const Edge &e) const { return w < e.w; }
} edges[M];
int main() {
  cin >> n >> m;
  init();
  for (int i = 0; i < m; ++i) {
    auto &[a, b, w] = edges[i];
    cin >> a >> b >> w;
  }
  sort(edges, edges + m);
  int cnt = 0, res = 0;
  for (int i = 0; i < m; ++i) {
    auto [a, b, w] = edges[i];

    a = find(a), b = find(b);
    if (a == b) continue;
    par[a] = b;
    cnt++;
    res += w;
  }

  if (cnt == n - 1)
    cout << res << '\n';
  else
    cout << "impossible\n";
  return 0;
}
